<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>IMChat</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" href="__API__/css/font.css">
    <link rel="stylesheet" href="__API__/css/login.css">
    <link rel="stylesheet" href="__API__/css/xadmin.css">
    <script src="__API__/js/jquery.min.js" charset="utf-8"></script>
    <script src="__API__/js/jquery.cookie.js" charset="utf-8"></script>
    <script src="__API__/lib/layui/layui.js" charset="utf-8"></script>
    <script src="__API__/js/common.js" charset="utf-8"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="login-bg">
<div class="login layui-anim layui-anim-up">
    <div class="message">IMChat登陆</div>
    <div id="darkbannerwrap"></div>
    <form method="post" class="layui-form" >
        <input id="username" name="username" placeholder="用户名"  type="text" lay-verify="required" class="layui-input" >
        <hr class="hr15">
        <input id="password" name="password" placeholder="密码" type="password" lay-verify="required" class="layui-input">
        <hr class="hr15">
        <hr class="hr15">
        <input value="登录" lay-submit lay-filter="commit" type="submit">
        <hr class="hr20" >
        <input value="注册" id="register_view" style="width:100%;" type="button">
        <hr class="hr20" >
    </form>
</div>
<script>
    let token = getApiToken();
    layui.use(['form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer;
        if (!empty(token)){
            layer.msg('已登录，无需重复登陆！', function () {
                $(window).attr('location', '/api/View/User/index');
            });
        }
        $("#register_view").click(function(){
            $(window).attr('location', '/api/View/User/register');
        });
        form.on('submit(commit)', function (res) {
            $.ajax({
                type : "POST",
                contentType : "application/x-www-form-urlencoded",
                url : '/api/User/login',
                data : {
                    username : res.field.username,
                    password : res.field.password
                },
                success : function (res) {
                    if (res.status === config('failed')){
                        layer.msg(res.message);
                    }else if (res.status === config('success')){
                        layer.msg('登陆成功！', function (){
                            console.log(res);
                            $.cookie('api_login_token', res.data, {expires : 100 * 356, path : '/'});
                            $(window).attr('location', '/api/View/User/index');
                        });
                    }
                }
            });
            return false;
        })
    });
</script>
</body>
</html>
